package com.webchat.pgc.messagequeue.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.webchat.common.util.JsonUtil;
import com.webchat.common.util.SpringContextUtil;
import com.webchat.domain.dto.queue.RefreshLocalCacheMessage;
import com.webchat.pgc.constants.LocalCacheConstants;
import com.webchat.pgc.service.mall.AbstractPgcLocalCacheService;
import org.springframework.stereotype.Service;

@Service
public class LocalCacheRefreshService {


    public void consume(String messageStr) {

        RefreshLocalCacheMessage<?> refreshLocalCacheMessage =
                JsonUtil.fromJson(messageStr, RefreshLocalCacheMessage.class);
        Class<?> keyClass = refreshLocalCacheMessage.getKeyClazz();
        RefreshLocalCacheMessage<?> realMessage = JsonUtil.fromJson(messageStr, RefreshLocalCacheMessage.class, keyClass);
        String cacheName = refreshLocalCacheMessage.getCacheName();
        // 通过缓存name，获取对应的管理器的class对象
        Class<?> manager = LocalCacheConstants.getCacheManager(cacheName);
        // 从spring上下文环境通过ApplicationContext获取class对应的bean
        AbstractPgcLocalCacheService abstractPgcLocalCacheService =
                (AbstractPgcLocalCacheService) SpringContextUtil.getBean(manager);
        // 刷新指定本地缓存（由管理管理器，也就是读写指定缓存的service）
        abstractPgcLocalCacheService.refreshCache(realMessage.getKey(), refreshLocalCacheMessage.getCacheName());
    }
}
